<template>
  <div class="formDetail">
    <cheader :pageIndex="99"></cheader>
    <div class="bg">
      <img :src="bg" alt="">
    </div>
    <div class="p1-title">
      <div class="tWrap">
        <div class="t">
          <img src="@/common/img/ball1.png" alt="">
          <h2>上海交通大学防疫答疑问卷</h2>
          <i></i>
        </div>
      </div>
    </div>
    <div class="content">
      <ul>
        <!-- <li>
          <h1>1、您所在的您所在的您所在的您所在的您所在的您所在的您所在的您所在的</h1>
          <div class="chooseRadio">
            <input type="radio" id="one" value="One" v-model="picked">
            <label for="one">One</label>
            <br>
            <input type="radio" id="two" value="Two" v-model="picked">
            <label for="two">Two</label>
          </div>
        </li> -->
        <li v-for="(item, index) in list" :key="item.qid">
          <h2>{{index+1}}. {{item.question}}</h2>
          <div class="chooseRadio" v-if="item.qatype == 0">
            <div v-for="(blc) in item.answers" :key="blc.id" class="answerBlc">
              <input type="radio" :value="blc.id" v-model="resultArr[index].value" :id="blc.id">
              <label :for="blc.id">{{blc.content}}</label>
            </div>
          </div>
          <div class="chooseTextarea" v-if="item.qatype == 1">
            <textarea name="" id="" cols="30" rows="10" v-model="resultArr[index].value"></textarea>
          </div>
        </li>
        <!-- <li v-for="(item, index) in list" :key="item.qid">
          <h1>{{index+1}}、{{item.question}}</h1>
          <div class="chooseRadio" v-if="item.qatype == 0">
            <div v-for="(blc, coe) in item.answers" :key="blc.id">
              <input type="radio" value="blc.content" v-model="resultArr[coe].value">
              <h1>{{blc.content}}</h1>
            </div>
          </div>
        </li> -->
      </ul>
      <button @click="getResult" class="submitBtn" v-if="list.length">提交</button>
    </div>
    <cfooter></cfooter>
  </div>
</template>
<script>
import cheader from '@/components/cheader/cheader'
import cfooter from '@/components/cfooter/cfooter'
import api from '@/common/js/api'
export default {
  data () {
    return {
      bg: require('@/common/img/bg.png'),
      list: [],
      resultArr: []
    }
  },
  created () {
    this.getData()
  },
  methods: {
    getData () {
      let uid = JSON.parse(localStorage.getItem('userInfo')).id
      if (uid) {
        api.formDetail(uid).then(res => {
          console.log(res.data.data)
          if (res.data.result === '0') {
            let arr = res.data.data
            for (let i = 0; i < arr.length; i++) {
              let item = {
                value: '',
                type: arr[i].qatype,
                qid: arr[i].qid
              }
              this.resultArr.push(item)
            }
            this.list = arr
            console.log(this.resultArr)
          }
        })
      } else {
        this.$router.push({
          name: 'project1'
        }).catch((e) => console.log(''))
      }
    },
    getResult () {
      let arr = this.resultArr
      let answers = []
      let openanswer = {}
      let uid = JSON.parse(localStorage.getItem('userInfo')).id
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].value === '') {
          window.alert('请完整填写问卷后提交')
          return false
        }
      }
      for (let l = 0; l < arr.length; l++) {
        console.log(arr[l])
        if (arr[l].type === 0) {
          let obj = {
            qid: arr[l].qid,
            aid: arr[l].value
          }
          answers.push(obj)
        } else if (arr[l].type === 1) {
          let obj = {
            qid: arr[l].qid,
            content: arr[l].value
          }
          openanswer = obj
        }
      }
      if (uid) {
        api.formSubmit(uid, answers, openanswer).then(res => {
          if (res.data.result === '0') {
            this.$router.push({
              name: 'formList'
            }).catch((e) => console.log(''))
          }
        })
      } else {
        this.$router.push({
          name: 'project1'
        }).catch((e) => console.log(''))
      }
    }
  },
  components: {
    cheader,
    cfooter
  }
}
</script>
<style lang="scss" scoped>
  $tjBlue: #59bed9;
  .formDetail {
    background: no-repeat center top linear-gradient(#59bed9, transparent, transparent);
    .bg {
      width: 100%;
      height: 698px;
      position: absolute;
      top: -115px;
      left: 0;
      z-index: 1;
      // background-color: red;
      img {
        display: block;
        margin: 0 auto;
      }
    }
    .p1-title {
      width: 900px;
      margin: 0 auto;
      padding-top: 160px;
      position: relative;
      z-index: 2;
      .t {
        height: 50px;
        position: relative;
        display: inline-block;
        img {
          position: absolute;
          left: 0;
          top: 0;
        }
        h2 {
          font-size: 28px;
          color: #fff;
          position: relative;
          top: 14px;
          margin-left: 24px;
          // font-weight: bold;
        }
        i {
          width: 6px;
          height: 6px;
          position: absolute;
          right: -16px;
          bottom: 0px;
          background-color: #ffe75f;
        }
      }
      .b {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        position: relative;
        h3 {
          font-size: 20px;
          color: #fff;
          position: relative;
          z-index: 3;
        }
        i {
          color: #fff;
          font-size: 20px;
          font-style: normal;
          cursor: pointer;
        }
      }
    }
    .content {
      position: relative;
      z-index: 2;
      background-color: #fff;
      margin: 20px auto;
      width: 900px;
      min-height: 800px;
      border-top-left-radius: 20px;
      border-top-right-radius: 20px;
      box-sizing: border-box;
      padding: 40px 50px;
      ul {
        li {
          margin-bottom: 30px;
          h2 {
            font-size: 16px;
            color: #444;
            font-weight: bold;
            margin-bottom: 14px;
            line-height: 30px;
          }
          .answerBlc {
            margin-bottom: 10px;
            padding-left: 20px;
            input {
              cursor: pointer;
            }
            label {
              cursor: pointer;
            }
          }
          .chooseTextarea {
            textarea {
              resize: none;
              width: 420px;
              height: 150px;
            }
          }
        }
      }
      .submitBtn {
        width: 76px;
        height: 30px;
        line-height: 30px;
        background-color: #1fcbf8;
        color: #fff;
        border: 0 none;
        border-radius: 10px;
        cursor: pointer;
      }
    }
  }
</style>
